package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils

class EffectivenessOfContract_30206_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX_EXPLAINATION_1 = /.*起生效.*/
        String REGEX_EXPLAINATION_2 = /.*后生效.*/
        String REGEX_EXPLAINATION_3 = /.*确认有效，.*/

        def textPathMap = dependParams.textPathMap
        def JSONTextMap = dependParams.JSONTextMap
        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        try {
            if (resultVOs!=null && resultVOs.size()>0) {
                for (int i = 0; i < resultVOs.size(); i++) {
                    def resultVO = resultVOs.get(i)
                    def text = resultVO.text
                    if ( text =~ REGEX_EXPLAINATION_1 || text =~ REGEX_EXPLAINATION_2 || text =~ REGEX_EXPLAINATION_3 ){
                        if(text.contains("）")){
                            def rawPath = textPathMap.get(resultVO.childTaskSn)
                            def rawText = FileUtils.loadJson(rawPath)
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(resultVO.childTaskSn)
                            def start = resultVO.start
                            def end = resultVO.end
                            def startOffset = text.indexOf("）") + 1
                            TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                            ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, startOffset+1 , 0, fileTextStr, rawText)
                            ObjectNew.text = rawText.substring(start + startOffset, end  )
                            ObjectNew.businessValue = rawText.substring(start + startOffset, end )
                            trueValueSet.add(ObjectNew)
                            return trueValueSet
                        }else{
                            trueValueSet.add(resultVO)
                            return trueValueSet
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace()
        }

        if(!CollectionUtils.isEmpty(resultVOs) && resultVOs.size()>2){
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            return trueValueSet
        }

        return  resultVOs
    }

}
